#include "ReinhardPhotographicReproduction.h"

ReinhardPhotographicReproduction::ReinhardPhotographicReproduction(double _Ldmax, double _Lwmax, double _a): ToneReproductionModel(_Ldmax, _Lwmax), a(_a){
}

void ReinhardPhotographicReproduction::calculateLd(){
	//Ld is not used in Reinhard's photographic tone reproduction
}

void ReinhardPhotographicReproduction::adjustScene(vector<MyColor>* pixels){
	calculateLw(pixels);

	for(size_t i = 0; i < pixels->size(); i++){
		MyColor* iColor = &(pixels->at(i));

		if(iColor->r < 0){
			iColor->r = 0;
		}
		else if(iColor->r > 1){
			iColor->r = 1;
		}
		if(iColor->g < 0){
			iColor->g = 0;
		}
		else if(iColor->g > 1){
			iColor->g = 1;
		}
		if(iColor->b < 0){
			iColor->b = 0;
		}
		else if(iColor->b > 1){
			iColor->b = 1;
		}
     
		double Rxy = iColor->r * Ldmax;
		double Gxy = iColor->g * Ldmax;
		double Bxy = iColor->b * Ldmax;
     
		double Rs = (a/Lw) * Rxy;
		double Gs = (a/Lw) * Gxy;
		double Bs = (a/Lw) * Bxy;
     
		double Rr = Rs/(1+Rs);
		double Gr = Gs/(1+Gs);
		double Br = Bs/(1+Bs);
     
		double Rtar = Rr*Ldmax;
		double Gtar = Gr*Ldmax;
		double Btar = Br*Ldmax;
     
		iColor->r = Rtar/Ldmax;
		iColor->g = Gtar/Ldmax;
		iColor->b = Btar/Ldmax;
	}
}